// pages/ucenter/carSetting/carSetting.js
let app = getApp();
const util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    carNumber: '', //车牌
    carModel: '', //车型
    carModelImg: '', //车标
    VehicleLicenseID: 0,
    EngineCode: "", //发动机号
    IsViolate: 0, //是否提醒
    FirstRegisterDate: "", //首次上牌时间
    IsYearCheck: 0, //是否年检提醒
    InsureExpireDate: '', //保险到期日期
    IsInsure: 0, //是否保险提醒
    MaintainDate: '', //最近保养日期
    MaintainPeriod: 0, //MaintainPeriod
    IsMaintain: 0, //IsMaintain
    ViolateNum: 0, //违章条数
    NextYearCheckDate: '', //下次年检时间
    NextMaintainDate: '' //下次保养时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      VehicleLicenseID: options.id || 32
    });
    this.getCar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getCarReminder();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 年检
   */
  checkYearTap: function() {
    let query = "?VehicleLicenseID=" + this.data.VehicleLicenseID;
    query += "&FirstRegisterDate=" + this.data.FirstRegisterDate;
    query += "&IsYearCheck=" + this.data.IsYearCheck;
    wx.navigateTo({
      url: '/pages/ucenter/checkYearReminder/checkYearReminder' + query,
    })
  },
  /**
   * 保养
   */
  maintainTap: function() {
    let query = "?VehicleLicenseID=" + this.data.VehicleLicenseID;
    query += "&MaintainDate=" + this.data.MaintainDate;
    query += "&MaintainPeriod=" + this.data.MaintainPeriod;
    query += "&IsMaintain=" + this.data.IsMaintain;
    wx.navigateTo({
      url: '/pages/ucenter/maintainReminder/maintainReminder' + query,
    })
  },
  /**
   * 保险
   */
  inSureTap: function() {
    let query = "?VehicleLicenseID=" + this.data.VehicleLicenseID;
    query += "&InsureExpireDate=" + this.data.InsureExpireDate;
    query += "&IsInsure=" + this.data.IsInsure;
    wx.navigateTo({
      url: '/pages/ucenter/insureReminder/insureReminder' + query,
    })
  },
  /**
   * 违章
   */
  violationTap: function() {
    let query = "?carNumber=" + this.data.carNumber;
    query += "&VehicleLicenseID=" + this.data.VehicleLicenseID;
    query += "&EngineCode=" + this.data.EngineCode;
    query += "&IsViolate=" + this.data.IsViolate;

    wx.navigateTo({
      url: '/pages/ucenter/violationReminder/violationReminder' + query,
    })
  },
  /**
   * 获取车辆信息
   */
  getCar: function() {
    let self = this;
//    console.log("getCars")
    let url = app.globalData.apiRootUrl + '/User/VehicleLicense';
    app.httpRequest({
      url: url,
      data: {
        vehicleId: self.data.VehicleLicenseID
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0 && rdata.data) {


          self.setData({
            carNumber: rdata.data.PlateNo,
            carModel: rdata.data.Brand + ' ' + rdata.data.Model,
            carModelImg: app.globalData.apiImgUrl + 'car/m_' + rdata.data.BrandID + '_100.png'
          })
        } else {
          wx.showToast({
            title: '车辆信息获取失败',
            icon: 'none'
          })
        }
      }
    });
  },
  /**
   * 获取车辆信息
   */
  getCarReminder: function() {
    let self = this;
   // console.log("getCars")
    let url = app.globalData.apiRootUrl + '/Car/GetVehicleRemind';
    app.httpRequest({
      url: url,
      data: {
        vehId: self.data.VehicleLicenseID
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0 && rdata.data) {
          if (rdata.data.FirstRegisterDate.startsWith('1900')) {
            rdata.data.FirstRegisterDate = '';
          } else {
            rdata.data.FirstRegisterDate = util.formatDate(rdata.data.FirstRegisterDate, 'yyyy-MM-dd');
            rdata.data.NextYearCheckDate = util.formatDate(rdata.data.NextYearCheckDate, 'yyyy-MM-dd'); 
          }
          if (rdata.data.MaintainDate.startsWith('1900')) {
            rdata.data.MaintainDate = '';
          } else {
            rdata.data.MaintainDate = util.formatDate(rdata.data.MaintainDate, 'yyyy-MM-dd');
            rdata.data.NextMaintainDate = util.formatDate(rdata.data.NextMaintainDate, 'yyyy-MM-dd');
          }
          if (rdata.data.InsureExpireDate.startsWith('1900')) {
            rdata.data.InsureExpireDate = '';
          } else {
            rdata.data.InsureExpireDate = util.formatDate(rdata.data.InsureExpireDate, 'yyyy-MM-dd');
  
          }

          self.setData({
            VehicleLicenseID: rdata.data.VehicleLicenseID,
            EngineCode: rdata.data.EngineCode,
            IsViolate: rdata.data.IsViolate,
            FirstRegisterDate: rdata.data.FirstRegisterDate,
            IsYearCheck: rdata.data.IsYearCheck,
            InsureExpireDate: rdata.data.InsureExpireDate,
            IsInsure: rdata.data.IsInsure,
            MaintainDate: rdata.data.MaintainDate,
            MaintainPeriod: rdata.data.MaintainPeriod,
            IsMaintain: rdata.data.IsMaintain,
            ViolateNum: rdata.data.ViolateNum,
            NextYearCheckDate: rdata.data.NextYearCheckDate,
            NextMaintainDate: rdata.data.NextMaintainDate,
          });

        } else {

        }
      }
    });
  },
})